<?php
/**
 * webappkit admin controller kit
 */
class WakAdminKit extends WakEngineKit {

    protected function _setUp() {
        // i18n
        $this->response->chooseLangFrom(array('en','fr'),'en');
        $this->templates->setCriteria('lang',$this->response->lang);
        // auth
        // log in
        if (!$this->realm->login(true)) {
            // if no config set, then forward to configurator page
            if (!$this->toolHasConfig('realm')) {
                $this->_setAction('setrealm');
                return true;
            }
            throw new WakEngineErrorPage("auth required",401);
        }
        $this->response->user=$this->realm->getUser();
        // log out
        if ($_REQUEST['logout']) {
            $this->realm->logout();
            throw new WakEngineErrorPage("logged out",401);
        }
        return true;
    }

    /**
     * fallback controller
     */
    protected function _fallbackController() {
        // defining view
        $view=str_replace('/','.',$this->response->url->getPath());
        // checking for template
        if (!$this->templates->has($view)) {
            // no template => 404
            throw new WakEngineErrorPage("no page for $view",404);
        }

        $this->response->view=$view;
        return true;
    }/**/

    /**
     *
     */
    function index() {
        // $this->_listKits();
        return true;
    }

    /**
     * realm configurator page
     */
    function setRealm() {
        return true;
    }

    public function kits() {
        // setting breadcrumb links
        $this->response->addBreadCrumb('Kits','kits');
        // listing kits
        $this->response->kits=array();
        foreach (Webappkit::listKits() as $id) {
            $this->response->kits[$id]=new KitBean($id);

        }
        // echo '<pre>'; print_r($this->response->kits);
        return true;
    }

    public function kit($id, $action=null) {
        // kit analysis
        $this->response->kit=new KitBean($id);
        // setting breadcrumb links
        $this->response->addBreadCrumb('Kits','kits');
        foreach (explode('.',$id) as $part) {
            $kitpath.=$part.'.';
            $this->response->addBreadCrumb($part,'kit/'.substr($kitpath,0,-1));
        }
        // echo '<pre>'; print_r($this->response->kit);
        switch ($action) {
            case 'tests':
                $this->response->view='kit.tests';
                $this->response->tests=$this->response->kit->get('tests_report');
                $this->response->addBreadCrumb('Tests','kit/'.$id.'/tests');
                break;
            default:
                $this->response->tests=$this->response->kit->get('tests_report');
                break;
        }
        return true;
    }

}
?>
